using System.Collections;
using F8Framework.Core;
using F8Framework.F8ExcelDataClass;
using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using F8Framework.Launcher;
using UnityEngine.UI;

public class TongGameLauncher : MonoBehaviour
{
    IEnumerator Start()
    {
        // 初始化模块中心
        ModuleCenter.Initialize(this);

        // 初始化版本
        FF8.HotUpdate = ModuleCenter.CreateModule<HotUpdateManager>();

        // 按顺序创建模块，可按需添加
        FF8.Message = ModuleCenter.CreateModule<MessageManager>();
        FF8.Input = ModuleCenter.CreateModule<InputManager>(new DefaultInputHelper());
        FF8.Storage = ModuleCenter.CreateModule<StorageManager>();
        FF8.Timer = ModuleCenter.CreateModule<TimerManager>();
        FF8.Procedure = ModuleCenter.CreateModule<ProcedureManager>();
        FF8.Network = ModuleCenter.CreateModule<NetworkManager>();
        FF8.FSM = ModuleCenter.CreateModule<FSMManager>();
        FF8.GameObjectPool = ModuleCenter.CreateModule<GameObjectPool>();
        FF8.Asset = ModuleCenter.CreateModule<AssetManager>();
#if UNITY_WEBGL
        yield return AssetBundleManager.Instance.LoadAssetBundleManifest(); // WebGL专用，如果游戏中没有使用任何AB包加载资源，可以删除此方法的调用！
#endif
        FF8.Config = ModuleCenter.CreateModule<F8DataManager>();
        FF8.Audio = ModuleCenter.CreateModule<AudioManager>();
        FF8.Tween = ModuleCenter.CreateModule<Tween>();
        FF8.UI = ModuleCenter.CreateModule<UIManager>();
#if UNITY_WEBGL
        yield return F8DataManager.Instance.LoadLocalizedStringsIEnumerator(); // WebGL专用
#endif
        FF8.Local = ModuleCenter.CreateModule<Localization>();
        FF8.SDK = ModuleCenter.CreateModule<SDKManager>();
        FF8.Download = ModuleCenter.CreateModule<DownloadManager>();
        FF8.LogWriter = ModuleCenter.CreateModule<F8LogWriter>();

        // 初始化本地版本
        FF8.HotUpdate.InitLocalVersion();

        // 初始化远程版本
        yield return FF8.HotUpdate.InitRemoteVersion();

        // 初始化资源版本
        yield return FF8.HotUpdate.InitAssetVersion();

        // 检查需要热更的资源，总大小
        Tuple<Dictionary<string, string>, long> result = FF8.HotUpdate.CheckHotUpdate();
        var hotUpdateAssetUrl = result.Item1;
        var allSize = result.Item2;

        // 资源热更新
        FF8.HotUpdate.StartHotUpdate(hotUpdateAssetUrl, () =>
        {
            LogF8.Log("完成");
        }, () =>
        {
            LogF8.Log("失败");
        }, progress =>
        {
            LogF8.Log("进度：" + progress);
        });

        // 检查未加载的分包
        List<string> subPackage = FF8.HotUpdate.CheckPackageUpdate(GameConfig.LocalGameVersion.SubPackage);

        // 分包加载
        FF8.HotUpdate.StartPackageUpdate(subPackage, () =>
        {
            LogF8.Log("完成");
        }, () =>
        {
            LogF8.Log("失败");
        }, progress =>
        {
            LogF8.Log("进度：" + progress);
        });

        StartGame();
        yield break;
    }

    // 开始游戏
    public void StartGame()
    {
        Util.Unity.StartCoroutine(LoadAll());
    }

    IEnumerator LoadAll()
    {
        foreach (var item in FF8.Config.LoadAllAsync()) // 异步加载全部配置
        {
            yield return item;
        }
#if UNITY_EDITOR
        ReadExcel.Instance.LoadAllExcelData();
#endif

        FF8.UI.Initialize(UIStaticModule.Instance.UIconfigs);

        FF8.UI.SetCanvasScaler(null, CanvasScaler.ScaleMode.ScaleWithScreenSize, referenceResolution: new Vector2(1080, 1920),
            CanvasScaler.ScreenMatchMode.MatchWidthOrHeight, matchWidthOrHeight: 0f, referencePixelsPerUnit: 100f);

        FF8.Procedure.RunProcedureNode<MenuProcedure>();
    }

    void Update()
    {
        // 更新模块
        ModuleCenter.Update();
    }

    void LateUpdate()
    {
        // 更新模块
        ModuleCenter.LateUpdate();
    }

    void FixedUpdate()
    {
        // 更新模块
        ModuleCenter.FixedUpdate();
    }
}

